// page-billOfDocumentImg/bodImghome.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: 'https://www.gcjxglzx.com/upload/uploadImgTest',
    path: '', //文件本地路径
    name: '', //name标识符
    pathType: '', //自定义上传的目录路径
    identityNum: '', //身份证号
    invoiceForBuyNum:'', //发票编号
    contractForBuyNum:'', //合同编号
    certificate: app.globalData.registerImage[4], //合格证书或档案证书
    invoice: app.globalData.registerImage[5], //采购发票照片
    purchaseContract: app.globalData.registerImage[6], //采购合同照片
    purchaseContractSec:'',
    bigShelfNum: '',//设备出厂编号
    qylxr: false
  },
  getInvoiceForBuyNum: function (e) {
    this.setData({
      invoiceForBuyNum: e.detail.value
    });
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "purchaseContract"
          });
          self.setData({
            purchaseContract: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              purchaseContract: imgResponseUrl
            })
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "invoice"
          });
          self.setData({
            invoice: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              invoice: imgResponseUrl
            })
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "certificate"
          });
          self.setData({
            certificate: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              certificate: imgResponseUrl
            })
          };
        } else if (e.target.id == '3') {
          self.setData({
            purchaseContractSec: "purchaseContractSec"
          });
          self.setData({
            invoice: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              purchaseContractSec: imgResponseUrl
            })
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    var identityNum = that.data.identityNum;//身份证号
    var contractForBuyImg = that.data.purchaseContract; //采购合同照片
    var contractSecImg = that.data.purchaseContractSec;
    var invoiceForBuyImg = that.data.invoice; //采购发票照片
    var archivesImg = that.data.certificate; //合格证照片或档案证照片
    var bigShelfNum = that.data.bigShelfNum; //设备出厂编号（大架号）
    wx.request({
      url: 'https://www.gcjxglzx.com/billOfDocumentImg/save',
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum,
        identityNum: identityNum,
        invoiceForBuyNum: invoiceForBuyNum,
        contractForBuyNum: contractForBuyNum,
        contractForBuyImg: contractForBuyImg,
        invoiceForBuyImg: invoiceForBuyImg,
        archivesImg: archivesImg
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.status == '0') {
          wx.request({
            url: 'https://www.gcjxglzx.com/registerLicense/save',
            data: {
              factoryNum: bigShelfNum,
              identification: identityNum,
              state: '0'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: function (res) {
              if (res.data.status == '0') {
                wx.navigateBack({
                  delta: 4
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bigShelfNum: wx.getStorageSync("factoryNum"),
      identityNum: wx.getStorageSync('identification')
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})